package com.luoye.lj.util.excel;

import com.luoye.lj.util.excel.format.IValueFormat;
import com.luoye.lj.util.excel.format.ValueFormatAdapter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/6/27
 * Time: 12:33
 * To change this template use File | Settings | File Templates.
 */
public class DefaultReportExclUtils<T> implements IExclUtils {
    IValueFormat feeValueFormat=new ValueFormatAdapter(){
        @Override
        public String format(int value) {
            return new BigDecimal(value/100d).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
        }
    };
    private Map<String,DataFieldDescription> fieldsMap;
    private List<String> fieldList;//name和desc键值对
    private String reportName;

    public DefaultReportExclUtils(List<String[]> fieldList,String reportName) {
        this.reportName=reportName;
        if(fieldList==null){
            return;
        }
        this.fieldList=new ArrayList<>();
        //Set<String> keys = fieldMap.keySet();
        this.fieldsMap=new HashMap<String,DataFieldDescription>();
        for(String[] nad:fieldList){
            this.fieldList.add(nad[0]);
            this.fieldsMap.put(nad[0],new DataFieldDescription(nad[0],nad[1]));
        }
//        fieldsMap.put("id",new DataFieldDescription("id", "NO.", new IEntityFormatExpand<ReportDetail>() {
//            @Override
//            public String format(ReportDetail reportDetail, int i) {
//                return ""+(i+1);
//            }
//        }));
//        fieldsMap.put("restaurantId",new DataFieldDescription("rest_id","餐厅编号"));
//        fieldsMap.put("brandName",new DataFieldDescription("brandName","品牌"));
//        fieldsMap.put("restaurantName",new DataFieldDescription("restaurantName","名称"));
//        fieldsMap.put("targetTc",new DataFieldDescription("targetTc","目标TC"));
//        fieldsMap.put("tc",new DataFieldDescription("tc","实际TC"));
//
//        fieldsMap.put("avgReadyTime",new DataFieldDescription("avgReadyTime", "平均备餐耗时(秒)", new IEntityFormat<ReportDetail>() {
//            @Override
//            public String format(ReportDetail reportDetail) {
//                double value=reportDetail.getAvgReadyTime();
//                return new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
//            }
//        }));
//        fieldsMap.put("avgTakeTime",new DataFieldDescription("avgTakeTime", "平均取餐耗时(秒)", new IEntityFormat<ReportDetail>() {
//            @Override
//            public String format(ReportDetail reportDetail) {
//                double value=reportDetail.getAvgTakeTime();
//                return new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
//            }
//        }));
//        fieldsMap.put("saleAmount",new DataFieldDescription("saleAmount", "总金额", new IEntityFormat<ReportDetail>() {
//            @Override
//            public String format(ReportDetail reportDetail) {
//                long value=reportDetail.getSaleAmount();
//                return new BigDecimal(value/100d).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
//            }
//        }));
//        fieldsMap.put("subsidy",new DataFieldDescription("subsidy", "补贴", new IEntityFormat<ReportDetail>() {
//            @Override
//            public String format(ReportDetail reportDetail) {
//                long value=reportDetail.getSubsidy();
//                return new BigDecimal(value/100d).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
//            }
//        }));
//        fieldsMap.put("newUserCount",new DataFieldDescription("newUserCount","新增用户"));
//
//        this.fieldList = fieldList;
    }

    @Override
    public ExportData createSheet(List<?> details, SXSSFWorkbook wb, ExportData exportData) {
        int fromN = exportData.getFromN();
        int begin = exportData.getBegin();
        int MAX = exportData.getMAX();

        SXSSFSheet sh = wb.getSheet(this.reportName+fromN);
        if(sh==null){
            sh = wb.createSheet(this.reportName+fromN);
        }
        for(int rownum = 0; rownum < details.size(); rownum++){
            if(begin >= MAX){
                fromN++;
                sh = wb.createSheet(this.reportName+fromN);
                begin = 0;
            }
            Row row = sh.createRow(begin);
            begin+=1;
            T reportDetail = (T) details.get(rownum);

            int size=this.fieldList.size();
            int cellIndex=0;
            for(int i = 0; i < size; i++){
                DataFieldDescription dataFieldDescription=this.fieldsMap.get(fieldList.get(i));
                if(dataFieldDescription!=null) {
                    Cell cell = row.createCell(cellIndex);
                    String value=new ValueExtractor<T>(reportDetail).getValue(fieldList.get(i),dataFieldDescription.getFormat(),rownum);
                    cell.setCellValue(value);
                    cellIndex++;
                }
            }
        }
        exportData.setBegin(begin);
        exportData.setFromN(fromN);
        return exportData;
    }

    @Override
    public ExportData createHead(Map<String, ?> headData, SXSSFWorkbook wb, ExportData exportData) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);//左右靠右
        SXSSFSheet sh = wb.getSheet(this.reportName+exportData.getFromN());
        if(sh==null){
            sh = wb.createSheet(this.reportName+exportData.getFromN());
        }
        int begin = exportData.getBegin();
        Row row = sh.createRow(0);

        int size=fieldList.size();
        int cellIndex=0;
        for(int i = 0; i < size; i++){
            DataFieldDescription dataFieldDescription=fieldsMap.get(fieldList.get(i));
            if(dataFieldDescription!=null) {
                Cell cell = row.createCell(cellIndex);
                cell.setCellValue(dataFieldDescription.getText());
                cellIndex++;
            }
        }

        sh.createFreezePane( 0, 1, 0, 1);

        exportData.setBegin(begin+1);
        return exportData;
    }

    @Override
    public ExportData createTail(Map<String, ?> tailData, SXSSFWorkbook wb, ExportData exportData) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);//左右靠右

        String[] par = null;
        T reportStatistics= (T) tailData.get("reportStatistics");
        if(reportStatistics!=null){
            int begin = exportData.getBegin();
            int fromN = exportData.getFromN();
            int MAX = exportData.getMAX();
            SXSSFSheet sh = wb.getSheet(this.reportName+exportData.getFromN());
            if(sh==null){
                sh = wb.createSheet(this.reportName+exportData.getFromN());
            }
            if(begin >= MAX){
                fromN++;
                sh = wb.createSheet(this.reportName+fromN);
                begin = 0;
            }

            Row row1 = sh.createRow(begin);
            Cell cell1 = row1.createCell(0);
            cell1.setCellValue("总计：");

            for(int rownum = 0; rownum < 1; rownum++){
                Row row = sh.createRow(begin+1);
                int size=fieldList.size();
                int cellIndex=0;
                for(int i = 0; i < size; i++){
                    DataFieldDescription dataFieldDescription=fieldsMap.get(fieldList.get(i));
                    if(dataFieldDescription!=null) {
                        Cell cell = row.createCell(cellIndex);
                        String value=new ValueExtractor<T>(reportStatistics).getValue(fieldList.get(i),dataFieldDescription.getFormat(),rownum);
                        cell.setCellValue(value);
                        cellIndex++;
                    }
                }
                begin+=1;
            }

            //最后设置列宽
//            for(int i=0;i<fieldList.size();i++){
//                int width=columnWidths[i];
//                sh.setColumnWidth(i,width*224);
//            }
            exportData.setBegin(begin+1);
        }
        return exportData;
    }
}
